//
//  MainHeaderView.m
//  吃货·上海
//
//  Created by 高青松 on 15/11/30.
//  Copyright © 2015年 濮亚军. All rights reserved.
//

#import "MainHeaderView.h"
#import "UIImageView+WebCache.h"

@interface MainHeaderView()<UIScrollViewDelegate>
@property (nonatomic,strong)UIScrollView *scrollView;
@property (nonatomic,strong)UIPageControl *pageControl;
@property (nonatomic,strong)NSMutableArray *curArray;
@property (nonatomic,strong)NSTimer *timer;
@end

@implementation MainHeaderView
{
    CGFloat width;
    CGFloat height;
    NSInteger curPage;
}
- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithReuseIdentifier:reuseIdentifier];
    if (self)
    {
        self.frame = CGRectMake(0, 0, KScreenWidth, KScreenHeight/4);
        width = KScreenWidth;
        height = KScreenHeight;
        self.curArray = [NSMutableArray arrayWithCapacity:3];
        
        [self initScrollView];
        [self initPageControl];
    }
    return self;
}

// 初始化滚动视图
- (void)initScrollView{
    self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight/4)];
    self.scrollView.contentSize = CGSizeMake(width *3, height);
    self.scrollView.contentOffset = CGPointMake(width, 0);
    self.scrollView.delegate = self;
    self.scrollView.pagingEnabled = YES;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView.showsVerticalScrollIndicator = NO;
    [self creatCarouseFigureView];
    
    
    [self.contentView addSubview:self.scrollView];
}

// 创建三个视图到轮播上
- (void)creatCarouseFigureView{
    for (int i = 0; i < 3; i++) {
        UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(width *i, 0, width, height/4)];
        [self.scrollView addSubview:imageView];
    }
}


// 初始化pageControl视图
- (void)initPageControl
{
    self.pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake((KScreenWidth - 80)/2, KScreenHeight/4 - 30, 80, 30)];
    self.pageControl.currentPageIndicatorTintColor = [UIColor greenColor];
    [self.contentView addSubview:self.pageControl];
}

// 更新当前的视图对应的索引
- (NSInteger)updatePage:(NSInteger)page
{
    NSInteger count = self.array.count;
    return  (count + page)%count;
}

// 更新当前对应的视图
- (void)updateCurrentViewWithCurPage:(NSInteger)page
{
    [self.curArray removeAllObjects];
    NSInteger pre = [self updatePage:page - 1];
    curPage = [self updatePage:page];
    NSInteger last = [self updatePage:page + 1];
    
    [self.curArray addObject:self.array[pre]];
    [self.curArray addObject:self.array[curPage]];
    [self.curArray addObject:self.array[last]];
    
    NSArray *subArray = self.scrollView.subviews;
    for (int i = 0; i < 3; i++) {
        UIImageView *imageView = subArray[i];
        imageView.image = [UIImage imageNamed:self.curArray[i]];
    }
    self.scrollView.contentOffset = CGPointMake(width, 0);
    self.pageControl.currentPage = curPage;
    
}


- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat x = scrollView.contentOffset.x;
    if (x >= width *2)
    {
        [self updateCurrentViewWithCurPage:curPage+1];
    }
    else if ( x <= 0 )
    {
        [self updateCurrentViewWithCurPage:curPage - 1];
    }
}

- (void)setArray:(NSArray *)array
{
    _array = array;
    self.pageControl.numberOfPages = array.count;
    [self updateCurrentViewWithCurPage:0];
    self.timer = [NSTimer scheduledTimerWithTimeInterval:4 target:self selector:@selector(autoPlay) userInfo:nil repeats:YES];
}

- (void)autoPlay
{
    [self.scrollView setContentOffset:CGPointMake(width *2,0) animated:YES];
}
@end
